import redis
import json
import sys


from .settings import DB, logging, HOST, PORT, PWD


def connect_redis():
    global db_cn
    try:
        db_cn = redis.Redis(host=HOST, port=PORT, db=DB, decode_responses=True, password=PWD)
    except Exception as e:
        logging.error(e)
    return db_cn


def insert_data_redis(rows, redis_name):
    """存任务"""
    redis_db = connect_redis()
    pipe = redis_db.pipeline()
    for row in rows:
        pipe.sadd(redis_name, row)
    pipe.execute()


def read_txt(txt_path, seq):
    total_list = []
    with open(txt_path, 'r', encoding='utf-8') as f:
        content = f.readlines()
    for con in content:
        cc = con.replace('\n', '').split(seq)
        keyword = cc[1] if cc[1] != '' else cc[0]
        total_list.append(keyword)
    return total_list


prov_li = [11, 12, 13, 14, 15, 21, 22,23,
31, 32, 33, 34, 35, 36, 37, 41, 42, 43,
44, 45, 46, 50, 51, 52, 53, 54, 61, 62,
63, 64, 65]


def load_info_local():
    with open('./new_list.csv', 'r', encoding='utf-8') as f:
        datas = f.readlines()
    datas = [x.strip() for x in datas]
    rows = []
    for prov in prov_li:
        for name in datas:
            rows.append(name+'\u0001{}'.format(prov))
    insert_data_redis(rows, '')


if __name__ == '__main__':
    # redis_name = 'XiaMen'
    # path_ = r'D:\python-running\taxpayer_status\Xiamen_ent.csv'
    # rows = read_txt(path_, ',')
    # insert_data_redis(rows, redis_name)
    pass